package view;

import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Window;


public class ScreenManager {

	private GraphicsDevice vc;
	private DisplayMode dm;
	private DisplayMode origDm;
	private View ecrans;
	private static final DisplayMode modes1[] = {

	new DisplayMode(1440, 1050, 64, 0), new DisplayMode(1440, 1050, 32, 0),
			new DisplayMode(1440, 900, 32, 0),
			new DisplayMode(1440, 900, 24, 0),
			new DisplayMode(1440, 900, 16, 0),
			new DisplayMode(1280, 1024, 32, 0),
			new DisplayMode(1280, 1024, 24, 0),
			new DisplayMode(1280, 1024, 16, 0),
			new DisplayMode(1280, 960, 32, 0),
			new DisplayMode(1280, 960, 24, 0),
			new DisplayMode(1280, 960, 16, 0),
			new DisplayMode(1280, 800, 32, 0),
			new DisplayMode(1280, 800, 24, 0),
			new DisplayMode(1280, 800, 16, 0),
			new DisplayMode(1152, 720, 32, 0),
			new DisplayMode(1152, 720, 24, 0),
			new DisplayMode(1152, 720, 16, 0),
			new DisplayMode(1024, 768, 32, 0),
			new DisplayMode(1024, 768, 24, 0),
			new DisplayMode(1024, 768, 16, 0),
			new DisplayMode(800, 600, 32, 0), new DisplayMode(800, 600, 24, 0),
			new DisplayMode(800, 600, 16, 0), new DisplayMode(640, 480, 32, 0),
			new DisplayMode(640, 480, 24, 0), new DisplayMode(640, 480, 16, 0) };

	public ScreenManager() {

		GraphicsEnvironment e = GraphicsEnvironment
				.getLocalGraphicsEnvironment();
		vc = e.getDefaultScreenDevice();

		dm = this.findFirstCompatibleMode(modes1);

	}

	 void updateRefToEcrans(View f) {
		this.ecrans = f;
	}

	void mettreEnPleinEcran() {
		origDm = getCurrentDisplayMode();
		setFullScreen(dm);
	}

	

	 DisplayMode[] getCompatibleDisplayModes() {
		return vc.getDisplayModes();
	}

	private DisplayMode findFirstCompatibleMode(DisplayMode modes[]) {
		DisplayMode goodModes[] = vc.getDisplayModes();
		for (int i = 0; i < modes.length; i++) {
			for (int j = 0; j < goodModes.length; j++) {
				if (displayModesMatch(modes[i], goodModes[j])) {
					return modes[i];
				}
			}
		}
		return null;
	}

	 DisplayMode getCurrentDisplayMode() {
		return vc.getDisplayMode();
	}

	private boolean displayModesMatch(DisplayMode dm1, DisplayMode dm2) {
		if (dm1.getWidth() != dm2.getWidth()
				|| dm1.getHeight() != dm2.getHeight()) {
			return false;
		}
		if (dm1.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI
				&& dm2.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI
				&& dm1.getBitDepth() != dm2.getBitDepth()) {
			return false;
		}
		if (dm1.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN
				&& dm2.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN
				&& dm1.getRefreshRate() != dm2.getRefreshRate()) {
			return false;
		}
		return true;
	}

	 void setFullScreen(DisplayMode dm) {
		ecrans.setVisible(true);
		vc.setFullScreenWindow(ecrans);

		if (dm != null && vc.isDisplayChangeSupported()) {
			try {
				vc.setDisplayMode(dm);
			} catch (Exception ex) {
			}
		}
	}

	 Window getFullScreenWindow() {
		return vc.getFullScreenWindow();
	}

	 int GetWidth() {
		Window w = vc.getFullScreenWindow();
		if (w != null) {
			return w.getWidth();
		} else {
			return 0;
		}
	}

	 int GetHeight() {
		Window w = vc.getFullScreenWindow();
		if (w != null) {
			return w.getHeight();
		} else {
			return 0;
		}
	}

	 void restore() {
		// MARCHE PAS !!!!
		vc.setFullScreenWindow(null);
		vc.setDisplayMode(origDm);
		ecrans.setSize(vc.getDisplayMode().getWidth(), vc.getDisplayMode()
				.getHeight() - 40);
		ecrans.repaint();
	}

	 void mettreEnModeFenetre() {
		
			Window w = vc.getFullScreenWindow();
			if (w != null && w == ecrans) {
				vc.setFullScreenWindow(null);
				vc.setDisplayMode(origDm);
				// ecrans.setVisible(true);
		
		}
		
	}

}
